import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router/index'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: localStorage.getItem('token') || null,
    userInfo: localStorage.getItem('user') || null,
    tagList: [
      { label: '首页', path: '/system/welcome', close: false }
    ]
  },
  mutations: {
    addTagList (state, obj) {
      state.tagList.push(obj)
    },
    closeTag (state, path) {
      const index = state.tagList.findIndex(item => item.path === path)
      let url = null
      if (index === state.tagList.length - 1) {
        url = state.tagList[index - 1].path
      } else {
        url = state.tagList[index + 1].path
      }
      router.push(`${url}`)
      state.tagList.splice(index, 1)
    }
  },
  actions: {
  },
  getters: {
    userImg (state) {
      let src = null
      src = JSON.parse(state.userInfo).avatar
      return src
    },
    userInfo (state) {
      const arr = []
      arr.push(JSON.parse(state.userInfo))
      return arr
    }

  },
  modules: {
  }
})
